package cn.sskxyz.security.authorization.service;

import cn.sskxyz.mybatis.mode.Page;
import cn.sskxyz.security.authorization.dao.SecurityServiceDao;
import cn.sskxyz.security.authorization.model.SecurityScope;
import cn.sskxyz.security.authorization.model.SecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProtectedResourceService {

    @Autowired
    private SecurityServiceDao securityServiceDao;

    public void addService(SecurityService securityService) {
        securityServiceDao.insertService(securityService);

    }

    public Page<List<SecurityService>> queryProtectedService(int currentPage, int pageSize) {
        Page page = new Page(currentPage, pageSize);
        List<SecurityService> services = securityServiceDao.selectProtectedService(page);
        page.setData(services);
        return page;
    }

    public void changeProtectedService(SecurityService securityService) {
        securityServiceDao.updateProtectedService(securityService);
    }

    public List<SecurityScope> queryServiceScope(int serviceId) {
        List<SecurityScope> scopes = securityServiceDao.selectServiceScope(serviceId);
        return scopes;
    }

    @CacheEvict(value = "client", allEntries = true)
    public void associateServiceScope(int serviceId, int scopeId, int status) {
        securityServiceDao.associateServiceScope(serviceId, scopeId, status);
    }

    @CacheEvict(value = "client", key = "#clientId")
    public void associateServiceClient(int serviceId, String clientId, int status) {
        securityServiceDao.associateServiceClient(serviceId, clientId, status);
    }
}
